package org.hismig.shard.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.classic.Session;
import org.hismig.migration.Context;
import org.hismig.utils.DateTool;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-9-6 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class DaoBase {

	private LocalSessionFactory sessionFactory;

	public void setSessionFactory(LocalSessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("rawtypes")
	public List findByHql(String hql) throws DaoException {

		return findByHql(hql, -1, -1);

	}

	@SuppressWarnings("rawtypes")
	public List findByHql(String hql, int firstResult, int maxResults)
			throws DaoException {

		return findByHql(hql, firstResult, maxResults,
				DateTool.getNormalDate(new Date()));

	}

	@SuppressWarnings("rawtypes")
	public List findByHql(String hql, String day) throws DaoException {

		return findByHql(hql, -1, -1, day);
	}

	@SuppressWarnings("rawtypes")
	public List findByHql(String hql, int firstResult, int maxResults,
			String day) throws DaoException {

		Context context = new Context();
		context.set(Context.KEY_CURRENTDAY, day);

		return findByHql(hql, firstResult, maxResults, context);

	}

	@SuppressWarnings("rawtypes")
	public List findByHql(String hql, Context context) throws DaoException {
		return findByHql(hql, -1, -1, context);
	}

	@SuppressWarnings("rawtypes")
	public List findByHql(String hql, int firstResult, int maxResults,
			Context context) throws DaoException {

		Session session = null;

		try {
			session = sessionFactory.openSession(context);

			Query query = session.createQuery(hql);

			if (firstResult >= 0) {
				query.setFirstResult(firstResult);
			}

			if (maxResults >= 0) {
				query.setMaxResults(maxResults);
			}

			return query.list();
		} catch (Throwable e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			if (null != session) {
				if (session.isConnected()) {
					try {
						session.close();
					} catch (HibernateException e) {
						e.printStackTrace();
					}
				}

				session = null;
			}
		}

	}

	public int executeByHql(String hql) throws DaoException {

		return executeByHql(hql, DateTool.getNormalDate(new Date()));

	}

	public int executeByHql(String hql, String day) throws DaoException {

		Context context = new Context();
		context.set(Context.KEY_CURRENTDAY, day);

		return executeByHql(hql, context);
	}

	public int executeByHql(String hql, Context context) throws DaoException {

		Session session = null;

		try {
			session = sessionFactory.openSession(context);

			return session.createQuery(hql).executeUpdate();

		} catch (Throwable e) {
			throw new DaoException(e.getMessage(), e);
		} finally {

			if (null != session) {
				if (session.isConnected()) {
					try {
						session.close();
					} catch (HibernateException e) {
						e.printStackTrace();
					}
				}

				session = null;
			}
		}
	}

	@SuppressWarnings("rawtypes")
	public List findBySql(String sql) throws DaoException {
		return findBySql(sql, -1, -1);
	}

	@SuppressWarnings("rawtypes")
	public List findBySql(String sql, int firstResult, int maxResults)
			throws DaoException {
		return findBySql(sql, firstResult, maxResults,
				DateTool.getNormalDate(new Date()));
	}

	@SuppressWarnings("rawtypes")
	public List findBySql(String sql, String day) throws DaoException {

		return findBySql(sql, -1, -1, day);
	}

	@SuppressWarnings("rawtypes")
	public List findBySql(String sql, int firstResult, int maxResults,
			String day) throws DaoException {

		Context context = new Context();
		context.set(Context.KEY_CURRENTDAY, day);

		return findBySql(sql, firstResult, maxResults, context);
	}

	@SuppressWarnings("rawtypes")
	public List findBySql(String sql, Context context) throws DaoException {
		return findBySql(sql, -1, -1, context);
	}

	@SuppressWarnings("rawtypes")
	public List findBySql(String sql, int firstResult, int maxResults,
			Context context) throws DaoException {
		Session session = null;

		try {
			session = sessionFactory.openSession(context);

			SQLQuery query = session.createSQLQuery(sql);

			if (firstResult >= 0) {
				query.setFirstResult(firstResult);
			}

			if (maxResults >= 0) {
				query.setMaxResults(maxResults);
			}

			return query.list();

		} catch (Throwable e) {
			throw new DaoException(e.getMessage(), e);
		} finally {

			if (null != session) {
				if (session.isConnected()) {
					try {
						session.close();
					} catch (HibernateException e) {
						e.printStackTrace();
					}
				}

				session = null;
			}
		}
	}

}
